﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Cryptography;
namespace DAL
{
    public class Crypt
    {
        private static string DES_SECRETKEY = "ThisIsSecretKey";
        public Byte[] GetByteFromString(string origin)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(origin);
            return byteArray;
        }
        public string Encrypt(string toEncrypt)
        {
            byte[] toEncryptArray =GetByteFromString(toEncrypt);
            var hashMd5 = new MD5CryptoServiceProvider();
            byte[] keyArray = hashMd5.ComputeHash(GetByteFromString(DES_SECRETKEY));
            var tDES = new TripleDESCryptoServiceProvider
            {
                Key=keyArray,
                Mode=CipherMode.ECB,
                Padding=PaddingMode.PKCS7
                
            };
            ICryptoTransform iTransform = tDES.CreateEncryptor();
            byte[] resultArray = iTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            hashMd5.Clear();
            tDES.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length).TrimEnd('=');
        }
        public string Decrypt(string toDecrypt)
        {
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt+"=");
            var hashMd5 = new MD5CryptoServiceProvider();
            byte[] keyArray = hashMd5.ComputeHash(GetByteFromString(DES_SECRETKEY)); 
            var tDES = new TripleDESCryptoServiceProvider
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            ICryptoTransform cTransform = tDES.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            hashMd5.Clear();
            tDES.Clear();
            return Encoding.UTF8.GetString(resultArray);
        }

        public static string GetMd5Hash(string inputString)
        {
            MD5 md5Hash = MD5.Create();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(inputString));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }

        public static string Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }
    }
}